package com.bbva.kyfb.kyfbjpatlbh.dominiotlbh;

import java.math.BigDecimal;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.dod.RooDataOnDemand;
import org.springframework.stereotype.Component;

/**
 * The Class TtlsbsfoDataOnDemand.
 */
@Configurable
@Component
@RooDataOnDemand(entity = Ttlsbsfo.class)
public class TtlsbsfoDataOnDemand {

	/** The rnd. */
	private Random rnd = new SecureRandom();

	/** The data. */
	private List<Ttlsbsfo> data;

	/**
	 * Gets the new transient ttlsbsfo.
	 *
	 * @param index the index
	 * @return the new transient ttlsbsfo
	 */
	public Ttlsbsfo getNewTransientTtlsbsfo(int index) {
        Ttlsbsfo obj = new Ttlsbsfo();
        setEmbeddedIdClass(obj, index);
        setAudFmodific(obj, index);
        setAudUsuario(obj, index);
        setCodBancopr(obj, index);
        setCodComrefex(obj, index);
        setCodEstadods(obj, index);
        setCodProdserv(obj, index);
        setCodSercrubd(obj, index);
        setCodSubprodu(obj, index);
        setDesAsunacce(obj, index);
        setDesPath(obj, index);
        setImpMaxreg(obj, index);
        setQnuPesofir(obj, index);
        setTimFichero(obj, index);
        setXtiMultirem(obj, index);
        return obj;
    }

	/**
	 * Sets the embedded id class.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setEmbeddedIdClass(Ttlsbsfo obj, int index) {
		TtlsbsfoPK embeddedIdClass;
		if (String.valueOf(index).length() > 7){
			embeddedIdClass = new TtlsbsfoPK(BigDecimal.valueOf(index), "c" + String.valueOf(index).substring(0, 6), "c" + String.valueOf(index).substring(0, 2), "c" + index);
		}else{
			embeddedIdClass = new TtlsbsfoPK(BigDecimal.valueOf(index), "c" + index, "c" + index, "c" + index);
		}
        obj.setId(embeddedIdClass);
    }

	/**
	 * Sets the aud fmodific.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setAudFmodific(Ttlsbsfo obj, int index) {
        Date audFmodific = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setAudFmodific(audFmodific);
    }

	/**
	 * Sets the aud usuario.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setAudUsuario(Ttlsbsfo obj, int index) {
        String audUsuario = "audUsu_" + index;
        if (audUsuario.length() > 8) {
            audUsuario = audUsuario.substring(0, 8);
        }
        obj.setAudUsuario(audUsuario);
    }

	/**
	 * Sets the cod bancopr.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodBancopr(Ttlsbsfo obj, int index) {
        Short codBancopr = new Integer(index).shortValue();
        obj.setCodBancopr(codBancopr);
    }

	/**
	 * Sets the cod comrefex.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodComrefex(Ttlsbsfo obj, int index) {
        String codComrefex = "codComrefe_" + index;
        if (codComrefex.length() > 12) {
            codComrefex = codComrefex.substring(0, 12);
        }
        obj.setCodComrefex(codComrefex);
    }

	/**
	 * Sets the cod estadods.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodEstadods(Ttlsbsfo obj, int index) {
        String codEstadods = "c" + index;
        if (codEstadods.length() > 2) {
            codEstadods = codEstadods.substring(0, 2);
        }
        obj.setCodEstadods(codEstadods);
    }

	/**
	 * Sets the cod prodserv.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodProdserv(Ttlsbsfo obj, int index) {
        Short codProdserv = new Integer(index).shortValue();
        obj.setCodProdserv(codProdserv);
    }

	/**
	 * Sets the cod sercrubd.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodSercrubd(Ttlsbsfo obj, int index) {
        Short codSercrubd = new Integer(index).shortValue();
        obj.setCodSercrubd(codSercrubd);
    }

	/**
	 * Sets the cod subprodu.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodSubprodu(Ttlsbsfo obj, int index) {
        Short codSubprodu = new Integer(index).shortValue();
        obj.setCodSubprodu(codSubprodu);
    }

	/**
	 * Sets the des asunacce.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setDesAsunacce(Ttlsbsfo obj, int index) {
        String desAsunacce = "desAsunacce_" + index;
        if (desAsunacce.length() > 40) {
            desAsunacce = desAsunacce.substring(0, 40);
        }
        obj.setDesAsunacce(desAsunacce);
    }

	/**
	 * Sets the des path.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setDesPath(Ttlsbsfo obj, int index) {
        String desPath = "desPath_" + index;
        if (desPath.length() > 100) {
            desPath = desPath.substring(0, 100);
        }
        obj.setDesPath(desPath);
    }

	/**
	 * Sets the imp maxreg.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setImpMaxreg(Ttlsbsfo obj, int index) {
        BigDecimal impMaxreg = BigDecimal.valueOf(index);
        if (impMaxreg.compareTo(new BigDecimal("9999999999999.99")) == 1) {
            impMaxreg = new BigDecimal("9999999999999.99");
        }
        obj.setImpMaxreg(impMaxreg);
    }

	/**
	 * Sets the qnu pesofir.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setQnuPesofir(Ttlsbsfo obj, int index) {
        BigDecimal qnuPesofir = BigDecimal.valueOf(index);
        if (qnuPesofir.compareTo(new BigDecimal("9.99")) == 1) {
            qnuPesofir = new BigDecimal("9.99");
        }
        obj.setQnuPesofir(qnuPesofir);
    }

	/**
	 * Sets the tim fichero.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setTimFichero(Ttlsbsfo obj, int index) {
        Date timFichero = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setTimFichero(timFichero);
    }

	/**
	 * Sets the xti multirem.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setXtiMultirem(Ttlsbsfo obj, int index) {
        Character xtiMultirem = new Character('N');
        obj.setXtiMultirem(xtiMultirem);
    }

	/**
	 * Gets the specific ttlsbsfo.
	 *
	 * @param index the index
	 * @return the specific ttlsbsfo
	 */
	public Ttlsbsfo getSpecificTtlsbsfo(int index) {
        init();
        if (index < 0) index = 0;
        if (index > (data.size() - 1)) index = data.size() - 1;
        Ttlsbsfo obj = data.get(index);
        return Ttlsbsfo.findTtlsbsfo(obj.getId());
    }

	/**
	 * Gets the random ttlsbsfo.
	 *
	 * @return the random ttlsbsfo
	 */
	public Ttlsbsfo getRandomTtlsbsfo() {
        init();
        Ttlsbsfo obj = data.get(rnd.nextInt(data.size()));
        return Ttlsbsfo.findTtlsbsfo(obj.getId());
    }

	/**
	 * Modify ttlsbsfo.
	 *
	 * @param obj the obj
	 * @return true, if successful
	 */
	public boolean modifyTtlsbsfo(Ttlsbsfo obj) {
        return false;
    }

	/**
	 * Inits the.
	 */
	public void init() {
        data = Ttlsbsfo.findTtlsbsfoEntries(0, 10);
        if (data == null) throw new IllegalStateException("Find entries implementation for 'Ttlsbsfo' illegally returned null");
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<com.bbva.kyfb.kyfbjpatlbh.dominiotlbh.Ttlsbsfo>();
        for (int i = 0; i < 10; i++) {
            Ttlsbsfo obj = getNewTransientTtlsbsfo(i);
            try {
                obj.persist();
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> it = e.getConstraintViolations().iterator(); it.hasNext();) {
                    ConstraintViolation<?> cv = it.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            obj.flush();
            data.add(obj);
        }
    }
}
